/*
 * Copyright 2020-2099 sa-token.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mjk.manager.satoken.plugin.jwt;

import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import com.mjk.common.tools.encrypt.JWTUtils;
import com.mjk.common.util.StrUtils;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Sa-Token 整合 jwt -- Simple 简单模式
 *
 * @author click33
 * @since 1.30.0
 */
public class StpLogicJwtForSimple extends StpLogic {
	/**
	 * key：账号类型
	 */
	public static final String LOGIN_TYPE = "loginType";

	/**
	 * key：账号id
	 */
	public static final String LOGIN_ID = "loginId";

	/**
	 * key：登录设备类型
	 */
	public static final String DEVICE = "device";

	/**
	 * key：有效截止期 (时间戳)
	 */
	public static final String EFF = "eff";

	/**
	 * key：乱数 （ 混入随机字符串，防止每次生成的 token 都是一样的 ）
	 */
	public static final String RN_STR = "rnStr";

	/**
	 * Sa-Token 整合 jwt -- Simple模式
	 */
	public StpLogicJwtForSimple() {
		super(StpUtil.TYPE);
	}

	/**
	 * Sa-Token 整合 jwt -- Simple模式
	 *
	 * @param loginType 账号体系标识
	 */
	public StpLogicJwtForSimple(String loginType) {
		super(loginType);
	}

	/**
	 * 获取jwt秘钥
	 *
	 * @return /
	 */
	public String jwtSecretKey() {
		String keyt = getConfigOrGlobal().getJwtSecretKey();
		StrUtils.notNull(keyt, "请配置jwt秘钥");
		return keyt;
	}

	// ------ 重写方法 

	/**
	 * 创建一个TokenValue
	 */
	@Override
	public String createTokenValue(Object loginId, String device, long timeout, Map<String, Object> extraData) {
		Map<String, Object> params = new HashMap<>();
		params.put(LOGIN_TYPE, this.loginType);
		params.put(LOGIN_ID, loginId);
		params.put(RN_STR, SaFoxUtil.getRandomString(32));
		if (!CollectionUtils.isEmpty(extraData))
			params.putAll(extraData);
		return JWTUtils.createTokenWithExpirs(params, -1);
	}

	/**
	 * 获取当前 Token 的扩展信息
	 */
	@Override
	public Object getExtra(String key) {
		return getExtra(getTokenValue(), key);
	}

	/**
	 * 获取指定 Token 的扩展信息
	 */
	@Override
	public Object getExtra(String tokenValue, String key) {
		return JWTUtils.parseToken(tokenValue).get(key);
	}


	@Override
	public boolean getConfigOfIsShare() {
		// 为确保 jwt-simple 模式的 token Extra 数据生成不受旧token影响，这里必须让 is-share 恒为 false 
		// 即：在使用 jwt-simple 模式后，即使配置了 is-share=true 也不能复用旧 Token，必须每次创建新 Token 
		return false;
	}

	/**
	 * 重写返回：支持 extra 扩展参数
	 */
	@Override
	public boolean isSupportExtra() {
		return true;
	}

}
